package com.zjj.spring.read.xml.aop.proxy.cglib;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor{

	private static CglibProxy instance;
	
	public static CglibProxy newInstance() {
		if(instance == null) {
			instance = new CglibProxy();
		}
		return instance;
	}
	private CglibProxy() { }
	
	@SuppressWarnings("unchecked")
	public <T> T getProxy(Class<T> clazz) {
		return (T) Enhancer.create(clazz, this);
	}

	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {

		System.out.println(" --- CglibProxy invoke : before ---");
		Object object = methodProxy.invokeSuper(obj, args);
		System.out.println(" --- CglibProxy invoke : before ---");
		
		return "[CGLIB代理对返回值做的处理] - 真实返回值如下：[" + object + "]";
	}
	
	

}
